package com.jq.jqaicode.controller;

import cn.hutool.core.util.StrUtil;
import com.jq.jqaicode.common.BaseResponse;
import com.jq.jqaicode.common.ResultUtils;
import com.jq.jqaicode.model.entity.AiApp;
import com.jq.jqaicode.service.AiAppService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.util.Map;

@RestController
@RequestMapping("/app")
@Slf4j
public class AiAppController {

    @Autowired
    AiAppService aiAppService;

    /**
     * 生成应用
     * @param appId
     * @param message
     * @return
     */
    @GetMapping(value = "/chat/genCode", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<ServerSentEvent<String>> chatToGenCode(@RequestParam String appId,@RequestParam String message){
        return aiAppService.chatToGenCode(appId,message)
                .map(chunk -> ServerSentEvent.<String>builder().data(chunk).build()).concatWith(Mono.just(
                        // 发送结束事件
                        ServerSentEvent.<String>builder()
                                .event("done")
                                .data("")
                                .build()
                ));
    }

    /**
     * 使用langgraph生成应用
     * @param appId
     * @param message
     * @return
     */
    @GetMapping(value = "/chat/workflowGenCode", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<ServerSentEvent<String>> workflowGenCode(@RequestParam String appId,@RequestParam String message) {
        return aiAppService.workflowGenCode(appId,message)
                .map(chunk -> ServerSentEvent.<String>builder().data((String) chunk).build()).concatWith(Mono.just(
                // 发送结束事件
                ServerSentEvent.<String>builder()
                        .event("done")
                        .data("")
                        .build()
        ));
    }

    /**
     * 创建应用
     * @param message
     * @return
     */
    @GetMapping(value = "/saveApp")
    public BaseResponse<String> saveApp(@RequestParam String message){
        if(StrUtil.isBlank(message)){
            ResultUtils.runError("消息内容不能为空");
        }
        String appId=aiAppService.saveApp(message);
        return ResultUtils.success(appId,"创建应用成功");
    }

    /**
     * 查询应用信息
     * @param appId
     * @return
     */
    @GetMapping(value = "/info")
    public BaseResponse<AiApp> info(@RequestParam String appId){
        if(StrUtil.isBlank(appId)){
            ResultUtils.runError("appId不能为空");
        }
        return ResultUtils.success(aiAppService.info(appId),"创建应用成功");
    }

    /**
     * 选为精选案例
     * @return
     */
    @GetMapping(value = "/updateShow")
    public BaseResponse<?> updateShow(@RequestParam String appId){
        if(StrUtil.isBlank(appId)){
            ResultUtils.runError("应用id不能为空");
        }
        aiAppService.updateShow(appId);
        return ResultUtils.success("成功");
    }

    /**
     * 游标翻页
     * cursor 上次返回的最后一条 id
     */
    @GetMapping(value = "/cursorPage")
    public BaseResponse<?> cursorPage(@RequestParam String cursor,Integer size){
        Map<String,Object> map=aiAppService.cursorPage(cursor,size);
        return ResultUtils.success(map,"成功");
    }
}
